package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbean.module.NeedProduct;
import com.tyqx.hashchainbiz.service.NeedProductService;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchaindao.mapper.NeedProductMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("needProductService")
public class NeedProductServiceImpl implements NeedProductService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private NeedProductMapper needProductMapper;

    @Async
    public Integer needProduct(NeedProduct needProduct) throws Exception{
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        needProduct.setCreateTime(createTime);
        needProduct.setUpdateTime(createTime);
        String needProductStr= needProduct.toString();
        HashIndex hashIndex= HashValueUtil.getHashValue(needProductStr);
        Integer flag=hashIndexMapper.insert(hashIndex);
        needProduct.setHashValue(hashIndex.getHashValue());

        return needProductMapper.addNeedProduct(needProduct);
    }

    @Override
    public List<NeedProduct> getNeedProductById(String donateId, String userName) {
        return needProductMapper.getNeedProductById(donateId);
    }

    @Override
    public List<NeedProduct> getNeedProductByTime(Date startTime, Date endTime, String userName) {
        return needProductMapper.getNeedProductByTime(startTime, endTime, userName);
    }
}
